package com.boost.io.controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;

@Controller
@RequestMapping("/fileUpload")
public class UpLoadFile {
    private final Logger logger = LoggerFactory.getLogger(UpLoadFile.class);

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public ResponseEntity tomcatTest(@RequestParam(value = "file",required = false) String file) throws Exception {
        System.out.println("servlet");
        return new ResponseEntity("hello world", HttpStatus.OK);
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public ResponseEntity fileUpload(@RequestParam("file") MultipartFile file) throws Exception {
        logger.info("OprUploadFileRestApiController -> fileUpload");
        String uri = "D:\\upload";
        if (StringUtils.isBlank(uri)) {
            return new ResponseEntity("请检查环境变量文件上传的目录设置", HttpStatus.OK);
        }
        String fileName = null;
        String filePath = null;
        // 判断文件是否为空
        if (!file.isEmpty()) {
            try {
                fileName = file.getOriginalFilename();
                filePath = uri + "/" + fileName;
                logger.info("文件名:{} 文件路径:{} 保存到硬盘", fileName, filePath);
                file.transferTo(new File(filePath));
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("文件保存失败", e);
                return new ResponseEntity("文件保存失败", HttpStatus.OK);
            }
        }
        return new ResponseEntity("上传成功", HttpStatus.OK);
    }


    @RequestMapping(value = "/presentImage/{fileUuid}")
    public ResponseEntity presentImage(@PathVariable String fileUuid, HttpServletResponse response) throws Exception {
        logger.info("OprUploadFileRestApiController -> presentImage request: {}", JSONObject.toJSONString(fileUuid));
        String uri = "D:\\upload";
        if (StringUtils.isBlank(uri)) {
            return new ResponseEntity("请检查环境变量文件上传的目录设置", HttpStatus.OK);
        }
        String path = uri + "/" + fileUuid;
        File file = null;
        byte[] bytes = null;
        try {
            file = new File(path);
            //bytes = FileUtils.readFileToByteArray(file);
        } catch (Exception o) {
            return new ResponseEntity("文件不存在", HttpStatus.OK);
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", new String("ww".getBytes("utf-8"), "ISO8859-1"));
        return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);

    }
}
